{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, get all packages that we might need and setup your notebook plotting to be inline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pencil as pc\n",
    "\n",
    "from matplotlib import animation, rc\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get simulation objects and its extends, here I assume $N_x = N_y = N_z$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sim = pc.get_sim()\n",
    "\n",
    "L = sim.param['lxyz'][0]\n",
    "Nx = sim.dim.nx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, read in varlist to get all var files in a list, plus I get the number range of all files and estimate there simulation time via dsnap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "varlist = sim.get_varlist()\n",
    "Nvar = np.size(varlist)\n",
    "var_range = np.array([int(var[3:]) for var in varlist])\n",
    "var_range_simtime = np.array([sim.get_value('dsnap')*varNum for varNum in var_range])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, lets read all the data we want to plot in >>one<< array:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40\n"
     ]
    }
   ],
   "source": [
    "print(Nvar)\n",
    "rhop = np.zeros((Nvar, Nx, Nx))\n",
    "for ii,var in zip(var_range, varlist):\n",
    "    VAR = pc.read.var(sim=sim, trimall=True, quiet=True, var_file=var)\n",
    "    rhop[ii] = VAR.rhop[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a figure and use imshow to plot your data. Make sure to use animated=True to get animation capability and use extent to set physically meaningfull plot ranges.\n",
    "\n",
    "The function updatefig then updates the plot with a new snapshot by iterating the index ii.\n",
    "\n",
    "Use intgervall to specify the ms for each frame and frames to specify the number of frames to be plotted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "\n",
    "ii = -1\n",
    "im = ax.imshow(rhop[100], animated=True, vmin=1e-2, vmax=10, extent=[-L, L, -L, L])\n",
    "\n",
    "def updatefig(*args):\n",
    "    global ii\n",
    "    ii += 1\n",
    "    if ii >= Nvar: return im,\n",
    "    im.set_array(rhop[ii])\n",
    "    return im,\n",
    "\n",
    "ani = animation.FuncAnimation(fig, updatefig, interval=500, frames=Nvar-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Last step: lets have a look on the video."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "HTML(ani.to_html5_video())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And maybe we want to save it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mywriter = animation.FFMpegWriter(fps=10)\n",
    "ani.save('tmp.mp4', writer=mywriter)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
